package br.com.solutions.delivery.dao;

import java.util.List;

import br.com.solutions.delivery.facade.serialization.SerializationFacade;
import br.com.solutions.delivery.dao.intf.IDao;
import br.com.solutions.delivery.facade.serialization.intf.ISerializationFacade;
        
public class SerializationDao<T> implements IDao<T> {

    private ISerializationFacade<T> serializationFacade;
    
    private List<T> objects;

    public SerializationDao(String name){
        super();
        this.serializationFacade = new SerializationFacade<T>(name);
    }

    @Override
    public void save(T object) throws Exception {
            try {
                    this.getAll().add(object);
                    this.serializationFacade.saveObjects(objects);
            } catch (Exception e) {
                    e.printStackTrace();
            }

    }

    @Override
    public T find(String id) throws Exception{
            for(T object : this.getAll()){
                    object.equals(id);
            }
            return null;
    }

    @Override
    public void delete(T object)  throws Exception{
            try {
                    this.getAll().remove(object);
                    this.serializationFacade.saveObjects(objects);
            } catch (Exception e) {
                    e.printStackTrace();
            }

    }

    @Override
    public List<T> getAll() throws Exception {
            try {
                if (this.objects == null)
                        this.objects = this.serializationFacade.getObjects();
                return this.objects;
            } catch (Exception e){
                    e.printStackTrace();
            }
            return null;
    }

}
